var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function BaseSwitch(sp_img,zb,ev) {
        this.sp_img=sp_img;
        this.zb=zb;
        this.ev=ev;
        this.Container_constructor();
        this.init();
        //console.log(this.father.targetWidth);

    }

    var p = createjs.extend(BaseSwitch, createjs.Container);
    p.className = "BaseSwitch";
    p.oriScale;
    p.teem_runing=false;
    p.startX=null;
    p.startY=null;
    p.moveing_y=null;
    p.moveing_x=null;
    p.endX=null;
    p.endY=null;
    p.srcoll_index=0;
    p.oldx=false;
    p.gone=false;
    // public methods:
    p.init = function () {
        this.oriScale = this.scaleX;
        this.addChild(this.sp_img);
        this.sp_img.on('mousedown', this.onMousedown, this);
        this.sp_img.on('pressmove', this.onPressmove, this);
        this.sp_img.on('pressup', this.onPressup, this);
    }
    p.onMousedown = function (evt) {
        // console.log(this.teem_runing,"down");
        if(this.teem_runing){
            return false;
        }
        this.startX = evt.stageX;
        this.startY = evt.stageY;
        // console.log(this.startX);
    }
    p.onPressmove = function (evt) {
        if(this.teem_runing){
            return false;
        }
        if (!this.oldx) {
            this.oldx=this.x;
        }
        this.moveing_y=this.startY-evt.stageY;
        this.moveing_x=this.startX-evt.stageX;
        this.endX = evt.stageX;
        this.endY = evt.stageY;
        this.jieguo();
        // p.movedistance(this);
    }
    p.onPressup = function (evt) {
        if(this.teem_runing){
            return false;
        }
        this.endX = evt.stageX;
        this.endY = evt.stageY;
    }
    p.jieguo = function(){
        if(this.teem_runing){
            return false;
        }
        var distance=Math.abs(this.startX-this.endX);
        var that=this;
        if ((distance>(targetWidth/2)*0.05)) {
            if (this.startX>this.endX) {
                // console.log("左");
                this.up(0);
            }else{
                this.up(1);
                // console.log("右");
            }
            // alert(this.zb);
            return false;
        }else{
            this.teem_runing=0;
        }
    }
    p.up=function(a){
        var that=this;
        
        // console.log((this.zb+a))
        if ((this.zb+a)>4) {
            return false;
        }
        // createjs.Tween.get(dqpage.getChildByName("b"+(this.zb+a)))
        // .to({alpha:a}, 500, createjs.Ease.linear)
        // .call(function(){
        //     that.teem_runing=0;
        // })
        if (a<1) {
            createjs.Tween.get(this.ev.getChildByName("b"+(this.zb+a)))
            .to({x:-targetWidth}, 700, createjs.Ease.quadInOut)
            .call(function(){
                that.teem_runing=0;
                if ((that.zb+a)<2) {
                    if (that.gone) {
                        return false;
                    }
                    that.gone=true;
                    contentLayer.jumpTo("page3");
                    return false;
                }
            })
        }else{
           createjs.Tween.get(this.ev.getChildByName("b"+(this.zb+a)))
            .to({x:this.oldx}, 1500, createjs.Ease.backOut)
            .call(function(){
                that.teem_runing=0;
            })
        }
        
    }
    // add MyApp to myNameSpace
    window.eosi.website.BaseSwitch = createjs.promote(BaseSwitch, "Container");
//}());
}(window));





